
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
//import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author VO CHI DUNG
 */
public class Menu {

    public Menu(PlanTable p) {
        parent = p;
        Image[] imageArray = null;
        String[] stringArray = {"Them", "Chinh sua", "Xoa", "Xoa tat ca", "Cau hinh", "Huond dan"};
        Font readElementFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);

        listItem = new List("MENU", Choice.IMPLICIT, stringArray, imageArray);

        for (int i = 0; i < listItem.size(); i++) {
            listItem.setFont(i, readElementFont);
        }

        listItem.addCommand(GlobalVariableStatic.CMD_BACK);

        listItem.setCommandListener(parent);
    }

    public List GetForm() {
        return listItem;
    }

    public void HandleInput(Command c) {
        if (GlobalVariableStatic.CMD_BACK == c) {
            parent.Switch(GlobalVariableStatic.LISTWORK);
        } else if (List.SELECT_COMMAND == c) {
            switch (listItem.getSelectedIndex()) {
                case 0:// Insert
                    handleInsert();
                    break;
                case 1:// Edit
                    handleEdit();
                    break;
                case 2:// Delete
                    handleDelete();
                    break;
                case 3:// Delete All
                    handleDeleteAll();
                    break;
                case 4:// Config
                    parent.Switch(GlobalVariableStatic.CONGIGURATION);
                    break;
            }
        }
    }

    private void handleInsert()
    {
        parent.Switch(GlobalVariableStatic.INSERT);
    }

    private void handleEdit()
    {
        if (GlobalVariableStatic.SelectedWork >= 0) {
            if (parent.loadEditWork()) {
                parent.Switch(GlobalVariableStatic.EDIT);
            } else {
                Alert alert = new Alert("Error", "Load record task khong thanh cong", null, AlertType.INFO);
                parent.getDisplay().setCurrent(alert, listItem);
            }
        } else {
            Alert alert = new Alert("Thong Bao", "Ban chua chon cong viec", null, AlertType.INFO);
            parent.getDisplay().setCurrent(alert, listItem);
        }
    }

    private void handleDelete() {
        if (GlobalVariableStatic.SelectedWork >= 0) {
            parent.DeleteByID(GlobalVariableStatic.SelectedWork);
            Alert alert = new Alert("Thong Bao", "Xoa thanh cong", null, AlertType.INFO);
            parent.getDisplay().setCurrent(alert, parent.getListWork().GetForm());
        } else {
            Alert alert = new Alert("Thong Bao", "Ban chua chon cong viec", null, AlertType.INFO);
            parent.getDisplay().setCurrent(alert, listItem);
        }
    }

    private void handleDeleteAll()
    {
        parent.DeleteALL();
        Alert alert = new Alert("Thong Bao", "Xoa tat ca thanh cong", null, AlertType.INFO);
        parent.getDisplay().setCurrent(alert, parent.getListWork().GetForm());
    }

    private List listItem;
    private PlanTable parent;
}
